package com.itfreer.ui.base;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Typeface;
import android.util.AttributeSet;
import android.view.View;

import com.itfreer.ui.R;
import com.itfreer.ui.base.api.ISimpleView;
import com.itfreer.utils.StringUtils;
import com.itfreer.ui.base.api.IPower;

import androidx.appcompat.widget.AppCompatTextView;

/**
 * 自定义TextView，解决字体符号绑定问题
 */
public class TextView extends AppCompatTextView implements IPower, ISimpleView {

    private String fieldName;
    private Object fieldValue;

    private String fontName;
    private String securityID;
    private boolean securityVisible=true;

    public TextView(Context context) {
        super(context);
        setCustomTypeface();
    }

    public TextView(Context context, AttributeSet attrs) {
        super(context, attrs);
        getFontName(context, attrs);
        setCustomTypeface();
    }

    public TextView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        getFontName(context, attrs);
        setCustomTypeface();
    }

    protected void getFontName(Context context, AttributeSet attrs){
        TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.SimpleView);
        fontName = a.getString(R.styleable.SimpleView_fontName);
        securityID = a.getString(R.styleable.SimpleView_securityID);
        fieldName = a.getString(R.styleable.SimpleView_fieldName);
    }

    protected void setCustomTypeface(){
        if(StringUtils.IsNullOrEmpty(fontName)){
            fontName = "ui.ttf";
        }
        Typeface font = Typeface.createFromAsset(this.getContext().getAssets(), fontName);
        setTypeface(font);
    }

    /**
     * 设置字体名称
     * @param fn
     */
    public void setFontName(String fn){
        this.fontName = fn;
        this.setCustomTypeface();
    }

    /**
     * 获取权限ID
     * @return
     */
    public String getSecurityID(){
        return securityID;
    }

    /**
     * 没有权限时，设置是否可见
     * @param visible
     */
    public void setSecurityVisible(boolean visible){
        securityVisible = visible;
        if(visible){
            this.setVisibility(View.VISIBLE);
        }else{
            // 表示不可见并且不占用空间
            this.setVisibility(View.GONE);
        }
    }

    /**
     * 设置绑定的依据对象
     * @param name
     */
    @Override
    public void setFieldName(String name){
        this.fieldName = name;
    }

    /**
     * 获取绑定的依据对象
     * @return
     */
    @Override
    public String getFieldName(){
        return fieldName;
    }

    /**
     * 设置绑定的值
     * @param value
     */
    @Override
    public void setFieldValue(Object value){
        this.fieldValue = value;
        if(fieldValue==null){
            this.setText("");
        }else{
            this.setText(fieldValue.toString());
        }
    }

    /**
     * 获取当前的值
     * @return
     */
    @Override
    public Object getFieldValue(){
        fieldValue = this.getText().toString();
        return fieldValue;
    }
}